#include "RenzoPrecompiled.h"
#include "RenzoSceneCollada.h"

namespace Renzo
{
SceneCollada::SceneCollada(void)
{
	SceneIO::ext = "dae";
}

SceneCollada::~SceneCollada(void)
{
}

/**
 Call ColladaDOM library to parse Collada scene file
 */
SceneNode* SceneCollada::load(const String& file) {
	const char* fileName = file.c_str();

	DAE* dae = new DAE;

	daeInt res = dae->load(fileName);
	if (res != DAE_OK)
	{
		rzDel(dae);
		throw Exception("Error loading the COLLADA file %s make sure it is COLLADA 1.4 or greater\n", fileName);		
	}
	
	domCOLLADA* dom = dae->getDom(fileName);
	if ( !dom )
	{
		rzDel(dae);
		throw Exception("COLLADA File loaded to the dom, but query for the dom assets failed \n");		
	}

	// Determine what vector x y or z is up. Typically y or z. 
	if ( dom->getAsset()->getUp_axis() )
	{			
		domAsset::domUp_axis * up = dom->getAsset()->getUp_axis();
		switch( up->getValue() )
		{
			case UP_AXIS_X_UP:			
				//CrtPrint("	X Axis is Up axis! default camera is adjusted\n" ); 
				//_CrtRender.SetUpAxis(eCrtXUp); 
				break; 
			case UP_AXIS_Y_UP:
				//CrtPrint("	Y Axis is Up axis!n" ); 
				//_CrtRender.SetUpAxis(eCrtYUp); 
				break;
			case UP_AXIS_Z_UP:
				//CrtPrint("	Z Axis is Up axis! default camera is adjusted\n" ); 
				//_CrtRender.SetUpAxis(eCrtZUp); 
				break; 
			default:			
				break; 
		}
	}

	//
	// Geometry libraries
	//
	domLibrary_geometries_Array domLibGeoArr = dom->getLibrary_geometries_array();
	for (int i = 0; i < domLibGeoArr.getCount(); ++i) {
		domLibrary_geometries* domLibGeo = domLibGeoArr[i];
		this->loadLibraryGeometries(domLibGeo);
	}
	
	return 0;
}

void SceneCollada::loadLibraryGeometries(domLibrary_geometries* lib) {
	domGeometry_Array domGeoArr = lib->getGeometry_array();
	for (int i = 0; i < domGeoArr.getCount(); ++i) {
		this->loadGeometry(domGeoArr[i]);
	}
}

void SceneCollada::loadGeometry(domGeometry* geo) {
	// now only support mesh 
	domMeshRef mesh = geo->getMesh();
	//mesh->
}

}
